package com.example.hospital.mapper;



import com.example.hospital.pojo.Schedule;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface ScheduleMapper {

    @Select("SELECT * FROM schedule WHERE user_id = #{userId}")
    List<Schedule> findByUserId(Integer userId);

    @Select("SELECT * FROM schedule")
    List<Schedule> findAll();

    @Select("SELECT * FROM schedule WHERE id = #{id}")
    Schedule findById(Integer id);

    @Insert("INSERT INTO schedule(user_id, department_id, position_id, shift_date, shift_type, created_at, updated_at) " +
            "VALUES(#{userId}, #{departmentId}, #{positionId}, #{shiftDate}, #{shiftType}, NOW(), NOW())")
    /*由于表中设置自增的主键ID, 这里设置useGeneratedKeys  = true 让MyBatis在插入数据后自动获取数据库生成的主键值
    * 设置keyProperty = "id" 表示将生成的主键值赋给Schedule对象的id属性 */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertSchedule(Schedule schedule);

    @Update("UPDATE schedule SET user_id=#{userId}, department_id=#{departmentId}, position_id=#{positionId}, " +
            "shift_date=#{shiftDate}, shift_type=#{shiftType}, updated_at=NOW() WHERE id=#{id}")
    void updateSchedule(Schedule schedule);

    @Delete("DELETE FROM schedule WHERE id=#{id}")
    void deleteSchedule(Integer id);

    @Select("SELECT * FROM schedule WHERE shift_date BETWEEN #{startDate} AND #{endDate}")
    List<Schedule> findByDateRange(@Param("startDate") Date startDate, @Param("endDate") Date endDate);
}
